package com.example.ele_guarantee_hh_pro.timejob;

import com.cloudcc.deveditor.core.CCObject;
import com.cloudcc.deveditor.core.ServiceResult;
import com.cloudcc.deveditor.trigger.TriggerEditor;

import java.util.List;

/**
 * 正式环境，等待运行，用完就删。
 */
public class test extends TriggerEditor {

    /**
     * 电子保函商机开函信息。
     */
    public void insert() {
        String code = record_new.get("code") == null ? "" : record_new.get("code") + "";
        String fk_psmc = record_new.get("fk_psmc") == null ? "" : record_new.get("fk_psmc") + "";
        String fk_ccuser = record_new.get("fk_ccuser") == null ? "" : record_new.get("fk_ccuser") + "";
        String findAccountId = "select id from Account where invoiceTaxNo = '" + code + "' and is_deleted ='0' ";
        List<CCObject> account = this.cqlQuery("Account", findAccountId);
        if (account.size() > 0) {
            String accountId = account.get(0).get("id") == null ? "" : account.get(0).get("id") + "";
            record_new.put("fk_account", accountId);
        }
        String findpsmcId = "select id from psmc where uncode= '" + fk_psmc + "' and is_deleted='0' ";
        List<CCObject> psmc = this.cqlQuery("psmc", findpsmcId);
        if (psmc.size() > 0) {
            String psmcId = psmc.get(0).get("id") == null ? "" : psmc.get(0).get("id") + "";
            record_new.put("fk_psmc", psmcId);
        }
        String findCCuserId = "select id from ccuser where employeenum ='" + fk_ccuser + "' and is_deleted='0' ";
        List<CCObject> ccuser = this.cqlQuery("ccuser", findCCuserId);
        if (ccuser.size() > 0) {
            String userId = ccuser.get(0).get("id") == null ? "" : ccuser.get(0).get("id") + "";
            record_new.put("fk_ccuser", userId);
        }

    }

    /**
     * 电子保函客户生成管户小组
     */
    public void accountInsertManger() {
        String accountType = "202385551266A597FF1B";
        String findSql = "select id,ownerid,invoiceTaxNo from Account where recordtype ='" + accountType + "' and is_deleted='0' ";
        List<CCObject> account = this.cqlQuery("Account", findSql);
        if (account.size() > 0) {
            for (int i = 0; i < account.size(); i++) {
                String id = account.get(i).get("id") == null ? "" : account.get(i).get("id") + "";
                String invoiceTaxNo = account.get(i).get("invoiceTaxNo") == null ? "" : account.get(i).get("invoiceTaxNo") + "";
                String ownerid = account.get(i).get("ownerid") == null ? "" : account.get(i).get("ownerid") + "";
                CCObject manage_group = new CCObject("manage_group");
                manage_group.put("fk_account", id);
                manage_group.put("is_shared", "否");
                manage_group.put("ghzt", "未生效");
                manage_group.put("name", invoiceTaxNo);
                manage_group.put("ownerid", ownerid);
                manage_group.put("fk_ccuser", ownerid);
                manage_group.put("fk_psmc", "b042022884952B61A3gj");
                this.insert(manage_group);

            }
        }

    }

    /**
     * 电子保函客户生成管户小组
     */
    public void testt1() {
        String id1 = "0052020EED26920RFayh"; //唐蜜
        String id2 = "0052021D229F12DLpc9b"; //李俊利
        String id3 = "0052021C5BF10B6bue6T"; //涂晏
        String id4 = "0052021512C6861cpHCb"; //侯作栋
        String id5 = "005202042D8B4B6xrmHs"; //曹医亚
        String id6 = "00520204714FB5AH1GJ8"; //赵彬
        String id7 = "005202211D5ED37KDjPE"; //重庆公海池
        String jieduanTYPE1 = "3-客情调研";
        String jieduanTYPE2 = "4-落地开函";
        String jieduanTYPE3 = "5-丢单";
        /*查询潜客id 生成客户*/
        String findEleoppInfoList = "select id,qkmc from eleopp where qkmc is not null and khmc is null and is_deleted='0' ";
        List<CCObject> eleoppList = this.cqlQuery("eleopp", findEleoppInfoList);
        /*潜客转客户*/
        if (eleoppList.size() > 0) {
            for (int i = 0; i < eleoppList.size(); i++) {
                String qkmcId = eleoppList.get(i).get("qkmc") == null ? "" : eleoppList.get(i).get("qkmc") + "";
                String eleoppId = eleoppList.get(i).get("id") == null ? "" : eleoppList.get(i).get("id") + "";
                String findLeadInfo = "select * from Lead where id ='" + qkmcId + "' ";
                List<CCObject> lead = this.cqlQuery("Lead", findLeadInfo);
                if (lead.size() > 0) {
                    CCObject account = new CCObject("Account");
                    String province1 = lead.get(0).get("province1") == null ? "" : lead.get(0).get("province1") + ""; //省份
                    String fk_city_name1 = lead.get(0).get("fk_city_name1") == null ? "" : lead.get(0).get("fk_city_name1") + ""; //城市
                    String fk_country_name1 = lead.get(0).get("fk_country_name1") == null ? "" : lead.get(0).get("fk_country_name1") + ""; //区县
                    String socialcreditcode = lead.get(0).get("socialcreditcode") == null ? "" : lead.get(0).get("socialcreditcode") + ""; //统一社会信用代码
                    String qzkhly = lead.get(0).get("qzkhly") == null ? "" : lead.get(0).get("qzkhly") + ""; //客户来源
                    String fk_kh = lead.get(0).get("fk_kh") == null ? "" : lead.get(0).get("fk_kh") + ""; //推荐客户
                    String fk_yg = lead.get(0).get("fk_yg") == null ? "" : lead.get(0).get("fk_yg") + ""; // 推荐员工
                    String company = lead.get(0).get("company") == null ? "" : lead.get(0).get("company") + ""; // 潜客名称
                    String qzkhzt = lead.get(0).get("qzkhzt") == null ? "" : lead.get(0).get("qzkhzt") + ""; // 跟进状态
                    String ownerid = lead.get(0).get("ownerid") == null ? "" : lead.get(0).get("ownerid") + ""; // syr
                    account.put("province", province1);
                    account.put("fk_city_name", fk_city_name1);
                    account.put("fk_country_name", fk_country_name1);
                    account.put("invoiceTaxNo", socialcreditcode);
                    account.put("qzkhly", qzkhly);
                    account.put("fk_yg", fk_yg);
                    account.put("fk_kh", fk_kh);
                    account.put("name", company);
                    account.put("fk_kh", fk_kh);
                    account.put("follow_status", qzkhzt);
                    if (ownerid.equals(id1) ||
                            ownerid.equals(id2) ||
                            ownerid.equals(id3) ||
                            ownerid.equals(id4) ||
                            ownerid.equals(id5) ||
                            ownerid.equals(id6)) {
                        account.put("ownerid", ownerid);
                        ServiceResult insert = this.insert(account);
                        String accountId = insert.get("id") + "";
                        String updateEleoppAccount = "update eleopp set khmc = '" + accountId + "' where id ='" + eleoppId + "'";
                        this.cqlQuery("eleopp", updateEleoppAccount);
                    } else {
                        account.put("ownerid", id7);
                        this.insert(account);
                    }
                }
            }
        }

        /*客户生成管户小组*/
        String accountType = "202385551266A597FF1B";
        String findSql = "select id,ownerid,invoiceTaxNo from Account where recordtype ='" + accountType + "' and is_deleted='0' and ownerid in('" + id1 + "','" + id2 + "','" + id3 + "','" + id4 + "','" + id5 + "','" + id6 + "')";
        List<CCObject> account = this.cqlQuery("Account", findSql);
        if (account.size() > 0) {
            for (int i = 0; i < account.size(); i++) {
                String ghtype = "未生效";
                String id = account.get(i).get("id") == null ? "" : account.get(i).get("id") + "";
                String invoiceTaxNo = account.get(i).get("invoiceTaxNo") == null ? "" : account.get(i).get("invoiceTaxNo") + "";
                String ownerid = account.get(i).get("ownerid") == null ? "" : account.get(i).get("ownerid") + "";
                String findEleoppsqL = "select id,jieduan from eleopp where khmc ='" + id + "' and is_deleted='0' ";
                List<CCObject> eleoppListInfo = this.cqlQuery("eleopp", findEleoppsqL);
                if (eleoppListInfo.size() > 0) {
                    String jieduan = eleoppListInfo.get(0).get("jieduan") == null ? "" : eleoppListInfo.get(0).get("jieduan") + "";
                    if (jieduan.equals(jieduanTYPE1) || jieduan.equals(jieduanTYPE2)) {
                        ghtype ="已生效";
                        CCObject manage_group = new CCObject("manage_group");
                        manage_group.put("fk_account", id);
                        manage_group.put("is_shared", "否");
                        manage_group.put("ghzt", ghtype);
                        manage_group.put("name", invoiceTaxNo);
                        manage_group.put("ownerid", ownerid);
                        manage_group.put("fk_ccuser", ownerid);
                        manage_group.put("fk_psmc", "b042022884952B61A3gj");
                        this.insert(manage_group);
                    }else if (jieduan.equals(jieduanTYPE3)){
                        CCObject manage_group = new CCObject("manage_group");
                        manage_group.put("fk_account", id);
                        manage_group.put("is_shared", "否");
                        manage_group.put("ghzt", ghtype);
                        manage_group.put("name", invoiceTaxNo);
                        manage_group.put("ownerid", ownerid);
                        manage_group.put("fk_ccuser", ownerid);
                        manage_group.put("fk_psmc", "b042022884952B61A3gj");
                        ServiceResult insert = this.insert(manage_group);
                        String manageId = insert.get("id")+"";
                        CCObject updateManageGroup= new CCObject("manage_group");
                        ghtype ="已生效";
                        updateManageGroup.put("id",manageId);
                        updateManageGroup.put("ghzt", ghtype);
                        this.update(updateManageGroup);
                    }
                }else {
                    CCObject manage_group = new CCObject("manage_group");
                    manage_group.put("fk_account", id);
                    manage_group.put("is_shared", "否");
                    manage_group.put("ghzt", ghtype);
                    manage_group.put("name", invoiceTaxNo);
                    manage_group.put("ownerid", ownerid);
                    manage_group.put("fk_ccuser", ownerid);
                    manage_group.put("fk_psmc", "b042022884952B61A3gj");
                    this.insert(manage_group);
                }
            }
        }

        String findSqlAccount = "select id,ownerid,invoiceTaxNo from Account where recordtype ='" + accountType + "' and is_deleted='0' and ownerid = '00520204BCF43BCedL2l' ";
        List<CCObject> accountInfoList = this.cqlQuery("Account", findSqlAccount);
        /*更新成公海池*/
        if (accountInfoList.size()>0){
            for (int i = 0; i < accountInfoList.size(); i++) {
                String accouuntId = accountInfoList.get(i).get("id")==null?"":accountInfoList.get(i).get("id")+"";
                CCObject updateAccount = new CCObject("Account");
                updateAccount.put("id",accouuntId);
                updateAccount.put("follow_status","待分配");
                this.update(updateAccount);
            }
        }
    }
}
